
const db = require('db');

module.exports = async ctx => {
    const { connection, run } = db.init();

    try {
        ctx.status = 200;
        ctx.body = {
            list: await list(ctx.request.body, run),
        };
    } catch (error) {
        ctx.status = 500;
        ctx.body = error
    }

    connection.end();
}

/** 获取列表 */
async function list(body, run) {
    const sqlStr = `
        SELECT *
        FROM logging
        WHERE logging.group = '${body.group}' and logging.source = '${body.source}'
        ORDER BY logging.creationTime DESC
    `
    const result = await run(sqlStr);
    if (result.err) {
        throw result.err
    }

    return result.rows;
}
